package com.lbn.monitor.widget.calendar;

import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/14.
 */
public class CalendarObserver {

    private static FragmentManager fragmentManager;

    private static List<Pos>list;

//    private static CalendarAdapter mAdapter;

    private static ViewPager mViewPager;

    private static CalendarFragmentAdapter fragmentAdapter;

//    private static int mPos;

    public static void subscribe(final CalendarAdapter adapter, int pos){

        if(adapter==null){
            return;
        }

        synchronized (CalendarObserver.class) {

            if(list==null){
                list = new ArrayList<>();
            }

            for(int i =0;i<list.size();i++){
                if(list.size()>0&&list.get(i).getAdapter()==adapter&&list.get(i).getPos()==pos){
                    return;
                }
            }
            Pos mPos = new Pos();
            mPos.setAdapter(adapter);
            mPos.setPos(pos);

            adapter.getItem(pos).state = 1;
            adapter.notifyItemChanged(pos);

            list.add(mPos);

            if (list.size() > 2) {
                Pos oldPos = list.get(0);
                oldPos.getAdapter().getItem(oldPos.getPos()).state = 0;
                oldPos.getAdapter().notifyItemChanged(oldPos.getPos());
                list.remove(oldPos);
            }
//            if (mAdapter != null) {
//                mAdapter.getItem(mPos).state = 0;
//                mAdapter.notifyItemChanged(mPos);
//            }
//
//            if (adapter != null) {
//                adapter.getItem(pos).state = 1;
//                adapter.notifyItemChanged(pos);
//            }
//            mAdapter = adapter;
//            mPos = pos;
        }
    }


    public static CalendarFragment getFragment(int position){
        return (CalendarFragment)fragmentAdapter.getFragment(mViewPager,fragmentAdapter.getItemId(position));
    }

    public CalendarFragmentAdapter getFragmentAdapter() {
        return fragmentAdapter;
    }

    public static void setViewPager(ViewPager viewPager) {
        mViewPager = viewPager;
        fragmentAdapter = (CalendarFragmentAdapter) viewPager.getAdapter();
    }

    public static ViewPager getViewPager() {
        return mViewPager;
    }


    public static void clear(){
//        mPos = 0;
//        mAdapter = null;
        mViewPager = null;
        fragmentAdapter = null;
        fragmentManager = null;
        list = null;
    }


    public static void setFragmentManager(FragmentManager fragmentManager) {
        CalendarObserver.fragmentManager = fragmentManager;
    }

    public static FragmentManager getFragmentManager() {
        return fragmentManager;
    }

    public static List<Pos>  getData(){
////        CalendarData data = mAdapter.getData(mPos);
//        CalendarData [] data = new CalendarData[2];
//
//        for(int i=0;i<list.size();i++){
//            Pos pos = list.get(i);
//            data[i]=pos.getAdapter().getItem(pos.getPos());
//        }
        return list;
    }
}
